package cn.tedu.mall.order.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.domain.CsmallAuthenticationInfo;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.order.mapper.OmsCartMapper;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.model.OmsCart;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author BJ
 * @date 2022/11/2 20:33
 */
@DubboService
@Service
@Slf4j
public class OmsCartServiceImpl implements IOmsCartService {
    @Autowired
    private OmsCartMapper omsCartMapper;

    // 1.向购物车中添加sku商品信息
    @Override
    public void addCart(CartAddDTO cartDTO) {
    // 先判断购物车中是否有sku商品
        OmsCart omsCart = omsCartMapper.selectExitCart(getUserId(), cartDTO.getSkuId());
        if (omsCart == null){
            OmsCart newCart = new OmsCart();
            BeanUtils.copyProperties(cartDTO,newCart);
            newCart.setUserId(getUserId());
            omsCartMapper.saveCart(newCart);
        }else {
            // 存在商品，则数量增加
            omsCart.setQuantity(omsCart.getQuantity()+cartDTO.getQuantity());
            omsCartMapper.saveCart(omsCart);
        }
    }

    // 2.将购物车中的商品分页显示给用户
    @Override
    public JsonPage<CartStandardVO> listCarts(Integer page, Integer pageSize) {

        // 从第一页开始，设置分页条件，自动在sql语句后添加limit分页信息
        PageHelper.startPage(page,pageSize);
        List<CartStandardVO> list = omsCartMapper.selectCartsByUserId(getUserId());

        return JsonPage.restPage(new PageInfo<>(list));
    }

    // 3.根据商品id单个或批量删除购物车中的sku信息
    @Override
    public void removeCart(Long[] ids) {
        int rows = omsCartMapper.deleteCartByIds(ids);
        if (rows == 0){
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"商品已经删除或购物车为空！");
        }
    }

    // 4.清空购物车
    @Override
    public void removeAllCarts() {
        int rows = omsCartMapper.removeAllCart(getUserId());
        if (rows == 0){
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"商品已经删除或购物车为空！");
        }
    }

    // 5.根据用户id和skuId 删除购物车中的商品
    @Override
    public void removeUserCarts(OmsCart omsCart) {
        omsCartMapper.deleteCartsByUserIdAndSkuId(omsCart);
    }

    // 6.修改购物车中的数量
    @Override
    public void updateQuantity(CartUpdateDTO cartUpdateDTO) {
        OmsCart omsCart = new OmsCart();
        BeanUtils.copyProperties(cartUpdateDTO,omsCart);
        omsCartMapper.updateQuantityById(omsCart);
    }

    public CsmallAuthenticationInfo getUserInfo(){
        // 从SpringSecurity 上下文中获取用户信息
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken)
                SecurityContextHolder.getContext().getAuthentication();
        CsmallAuthenticationInfo csmallAuthenticationInfo =
                (CsmallAuthenticationInfo) authenticationToken.getCredentials();
        return csmallAuthenticationInfo;
    }
    public Long getUserId(){
        return getUserInfo().getId();
    }
}
